package com.roc.dao.merinfo;

import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.roc.dao.DaoException;
import com.roc.dao.impl.BaseDaoImpl;

/**
 * 商户复审dao
 * @author jdd
 *
 */
@Repository("merinfoChangeCheckDao")
public class MerinfoChangeCheckDaoImpl extends BaseDaoImpl  implements MerinfoChangeCheckDao{
	
	@Override
	public List queryBySqlToVo(String SQL,  Object... params) throws DaoException {
		// TODO Auto-generated method stub
		
	    Query q=sessionFactory.getCurrentSession().createSQLQuery(SQL);
		if (params != null && params.length>0) {
			int index = 0;
			for (int i = 0; i < params.length; i++) {
				if (params[i] instanceof Object[]) {
					Object[] oarr = (Object[]) params[i];
					for (int j = 0; j < oarr.length; j++) {
						q.setParameter(j + index, oarr[j]);
					}
					index = index + oarr.length;
				} else {
					q.setParameter(i, params[i]);
					index = index + 1;
				}
			}
		}
		return q.list();
	}

	@Override
	public int updateBySql(String sql, Object... params) throws DaoException {
		// TODO Auto-generated method stub
		 Query q=sessionFactory.getCurrentSession().createSQLQuery(sql);
			if (params != null && params.length>0) {
				int index = 0;
				for (int i = 0; i < params.length; i++) {
					if (params[i] instanceof Object[]) {
						Object[] oarr = (Object[]) params[i];
						for (int j = 0; j < oarr.length; j++) {
							q.setParameter(j + index, oarr[j]);
						}
						index = index + oarr.length;
					} else {
						q.setParameter(i, params[i]);
						index = index + 1;
					}
				}
			}
		return q.executeUpdate();
	}
	@Override
	public List<?> queryAll(Class clazz,Map<String, Object> param) throws DaoException 
	{
		try {
			
		
		Criteria criteria=sessionFactory.getCurrentSession().createCriteria(clazz);
		criteria.add(Restrictions.allEq(param));
		return criteria.list();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			throw new DaoException();
		}
	}
	
}
